package com.zry.service.collect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zry.VO.CollectVo;
import com.zry.entity.Collect;
import com.zry.entity.CollectCategory;
import com.zry.mapper.CollectMapper;
import com.zry.service.collectCategory.ICollectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service("CollectServiceImpl")
@Slf4j
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Autowired
    private ICollectCategoryService collectCategoryService;

    @Override
    public List<CollectVo> findCollectByCategoryId() {

        // 查出发布的分类
        LambdaQueryWrapper<CollectCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CollectCategory::getStatus, 1);
        List<CollectCategory> categoryList = collectCategoryService.list(lambdaQueryWrapper);

        List<CollectVo> collectVoList = categoryList.stream().map(category -> {
            CollectVo collectVo = new CollectVo();
            collectVo.setCategoryId(category.getId());
            collectVo.setCategoryTitle(category.getCategoryTitle());

            IPage<Collect> collectPage = getCollectPage(category.getId(), 1,8);

            List<Collect> records = collectPage.getRecords();

            if (CollectionUtils.isEmpty(records)) {
                collectVo.setCollectList(new ArrayList<>());
            }

            collectVo.setCollectList(records);

            Map<String, Object> pageMap = new HashMap();
            pageMap.put("total", collectPage.getTotal());
            pageMap.put("pages", collectPage.getPages());
            pageMap.put("pageNo", collectPage.getCurrent());
            pageMap.put("pageSize", collectPage.getSize());
            collectVo.setPageMap(pageMap);

            return collectVo;
        }).collect(Collectors.toList());


        return collectVoList;
    }


    @Override
    public List<Collect> findCollectByPageNo(Integer categoryId, Integer pageNo, Integer pageSize) {
        IPage<Collect> collectPage = getCollectPage(categoryId, pageNo, pageSize);

        List<Collect> records = collectPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new ArrayList<>();
        }

        return records;
    }

    /**
     * 获取分页内容
     */
    private IPage<Collect> getCollectPage(Integer categoryId, Integer pageNo, Integer pageSize) {
        IPage<Collect> page = new Page<>(pageNo, pageSize);

        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("category_id", categoryId);

        return this.page(page, queryWrapper);
    }


}
